#Importing Library and dataset
library(tidyverse)
library(plotly)
raw_data <- read.csv("Country_GDP.csv")

Filter, extract, and clean relevant data

c_code <- c("USA","EUU","CHN","JPN","CAN","BRA")
filt_data <- filter(raw_data,Country.Code %in% c_code)
colnames(filt_data) <- col_nam <- c("Country",
                                        "Country_Code",seq(2001,2020))

plot_data <- data.frame(t(filt_data[,3:22]))
colnames(plot_data) <- filt_data$Country_Code


#GDP Per Billion dollars
plot_data <- plot_data/1000000000
head(plot_data)
##           BRA      CAN     CHN      EUU     JPN     USA
## 2001  559.984  738.982 1339.40  7388.17 4374.71 10581.8
## 2002  509.795  760.649 1470.55  8050.02 4182.85 10936.4
## 2003  558.234  895.541 1660.29  9912.55 4519.56 11458.2
## 2004  669.289 1026.690 1955.35 11400.60 4893.12 12213.7
## 2005  891.634 1173.110 2285.97 11907.00 4831.47 13036.6
## 2006 1107.630 1319.260 2752.13 12704.70 4601.66 13814.6

Creating the plot

fig <- plot_ly(plot_data, type = 'scatter', mode = 'lines') %>%
  add_trace(x = seq(2001,2020), y = ~BRA, name = 'Brazil') %>%
  add_trace(x = seq(2001,2020), y = ~CAN, name = 'Canada') %>%
  add_trace(x = seq(2001,2020), y = ~CHN, name = 'China') %>%
  add_trace(x = seq(2001,2020), y = ~EUU, name = 'EU') %>%
  add_trace(x = seq(2001,2020), y = ~JPN, name = 'Japan') %>%
  add_trace(x = seq(2001,2020), y = ~USA, name = 'USA') %>%
  layout(title = "Major Countries GDP",
         legend = list(title=list(text='Countries'),x=-0.3,y=0), 
         xaxis = list(title = 'Date',
                      rangeslider = list(visible = T)),
         yaxis = list(title = 'GDP [$Billions]'),
         plot_bgcolor='#e5ecf6',
         
         updatemenus = list(
           list(
             buttons = list(
               list(method = 'restyle',
                    args = list('visible',list(TRUE,TRUE,TRUE,TRUE,TRUE,TRUE)),
                    label = "All"),
               
               list(method = 'restyle',
                    args = list('visible',list(TRUE,FALSE,FALSE,TRUE,TRUE,FALSE)),
                    label = "Super Powers"),
               
               list(method = 'restyle',
                    args = list('visible',list(TRUE,FALSE,TRUE,FALSE,FALSE,FALSE)),
                    label = "North America")))
           ))

fig